//
//  SYPopoverAnimation.swift
//  SingWeibo
//
//  Created by shiying on 16/1/1.
//  Copyright © 2016年 shiying. All rights reserved.
//

import UIKit

class SYPopoverAnimation: NSObject, UIViewControllerAnimatedTransitioning {

    // 转场动画的时间
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.25
    }
    
    // 当实现这个方法后，需要我们手动将元素添加到容器视图，充分地让我们处定义转场动画
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        // 将modal出来的控制器的view 添加到容器视图
        transitionContext.containerView()?.addSubview(toView)
        
        // 设置方向的缩放
        toView.transform = CGAffineTransformMakeScale(1, 0)
        
        // 设置锚点
        toView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
        
        // 动画
        UIView.animateWithDuration((transitionDuration(transitionContext)), animations: { () -> Void in
            toView.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                transitionContext.completeTransition(true)
        }
    }
}
